<?php
/**
 *  
 * @package package_name
 * @author Mervin.Wong<mervin0502@163.com>
 * @Copyright
 * @license  http://www.apache.org/licenses/LICENSE-2.0
 * @version 0.1.0
 * @date 2013-3-6下午7:47:56
 *
 */
/**
 * CanGoo Option Controller
 * 博客设置
 * @package admin
 * @subpackage controller
 * @category admin
 * @author mervin.mong
 */
  
  class Option extends CI_Controller{

		/**
		 * 构造函数
		 */
		public function __construct(){
			parent::__construct();

			$this->load->helper('form');
			$this->load->helper('url');
			$this->load->model('blog/option_model');
			
			$this->load->library('session');
			$this->load->model('common_model');
			if(!$this->common_model->is_login($this->session->userdata('session_id'))){
				redirect(site_url('index/login', 'refresh'));
			}
			$this->load->library('form_validation');
			
			//常规设置
			$this->form_validation->set_rules('general_title', '网站名称','trim|min_length[2]|max_length[20]|xss_clean');
			$this->form_validation->set_rules('general_description', '网站描述', 'trim|required|xss_clean|prep_form_url');
			$this->form_validation->set_rules('general_keywords', '关键词', 'trim|required');
			
			
			
		}  	
		
		public function index(){
			redirect(site_url('blog/option/general'), 'refresh');
		}
		
		/**
		 * 常规设置
		 * 功能项:站点名称，站点描述，关键词
		 */
		public function general(){
			
			$values['title'] = 'a';
			$values['description'] = 'a';
			$values['keywords'] = 'a';
			
			if($this->input->post('is_submit') == 'ok'){
				//提交
				if($this->form_validation->run() == TRUE){
					$values['title'] = $this->input->post('general_title');
					$values['description'] = $this->input->post('general_description');
					$values['keywords'] = $this->input->post('general_keywords');
					
					//通过验证提交到数据库
					if($row = $this->option_model->get_one('oid', 'options', array('name'=>'site_title'))){
						$this->option_model->update(array('value'=>$values['title']), 'options', array('oid'=>$row['oid']));
					}else{
						$this->option_model->insert(array('name'=>'site_title', 'value'=>$values['title'], 'module'=>'blog'), 'options');
					}
					if($row = $this->option_model->get_one('oid', 'options', array('name'=>'site_description'))){
						$this->option_model->update(array('value'=>$values['description']), 'options', array('oid'=>$row['oid']));
					}else{
						$this->option_model->insert(array('name'=>'site_description', 'value'=>$values['description'], 'module'=>'blog'), 'options');
					}
					if($row = $this->option_model->get_one('oid', 'options', array('name'=>'site_keywords'))){
						$this->option_model->update(array('value'=>$values['keywords']), 'options', array('oid'=>$row['oid']));
					}else{
						$this->option_model->insert(array('name'=>'site_keywords', 'value'=>$values['keywords'], 'module'=>'blog'), 'options');
					}
				}else{
					$values['title'] = set_value('general_title');
					$values['description'] = set_value('general_description');
					$values['keywords'] = set_value('general_keywords');
				}
			}else{
				$row = $this->option_model->get_one('value', 'options', array('name'=>'site_title'));
				$values['title'] = $row['value'];
				$row = $this->option_model->get_one('value', 'options', array('name'=>'site_description'));
				$values['description'] = $row['value'];
				$row = $this->option_model->get_one('value', 'options', array('name'=>'site_keywords'));
				$values['keywords'] = $row['value'];					
			}
			
			$form = form_open(site_url('blog/option/general'),array('name'=>'general_form', 'id'=>'general_form'));
			$form .= '<p>';
			$form .= form_label('站点名称:', 'general_title');
			$form .= form_input(array('name'=>'general_title', 'id'=>'general_title', 'size'=>16, 'value'=>$values['title']));
			$form .='</p><p>';
			$form .= form_label('站点描述:', 'general_description');
			$form .= form_textarea(array('name'=>'general_description', 'id'=>'general_description', 'width'=>'200px', 'height'=>'80px', 'value'=>$values['description']));
			$form .='</p><p>';
			$form .= form_label('关键词:', 'general_keywords');
			$form .= form_input(array('name'=>'general_keywords', 'id'=>'general_keywords', 'size'=>60, 'value'=>$values['keywords']));
			$form .='</p><p>';
			$form .= form_hidden('module', 'blog');
			$form .= form_hidden('is_submit', 'ok');
			$form .= form_reset('general_reset', '重写');
			$form .= form_submit('general_submit', '提交');
			$form .= form_close();
			
			$data['title'] = '常规设置';
			$data['form'] = $form;
			$this->load->view('blog/option_view', $data);
		}
		
  	
  }
/* End of file : option.php */
/* Location:./admin/controllers/blog/option.php */
